//
//  myInfoViewController.m
//  百车易车险
//
//  Created by chenxu on 15/3/7.
//  Copyright (c) 2015年 chenxu. All rights reserved.
//

#import "myInfoViewController.h"
#import "LeftImage_RightButton_Cell.h"
#import "changecarViewController.h"
#import "NetAPIManager.h"
@interface myInfoViewController ()

@end

@implementation myInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _curUser =[Login curLoginUser];
    
    _carlistview = [[CarListView alloc]initWithFrame:CGRectMake(0, -20, kScreen_Width, self.view.frame.size.height-83) cars:nil selectblock:^(Car *car) {
        [Login selectCar:car];
    }   modifyblock:^(Car *car) {
        [self performSegueWithIdentifier:@"modifyCarFromInfoView" sender:car];
    }   pushblock:^(){
        [self performSegueWithIdentifier:@"addCarFromInfoView" sender:nil];
    }];
    
    [self.carinfo addSubview:_carlistview];
    [self.view bringSubviewToFront:_userinfo];
    [self.view bringSubviewToFront:_segmentcontrol];
    [self.view setBackgroundColor:kColorTableBG];
    [_userinfotableview setBackgroundColor:kColorTableBG];
    _userinfotableview.tableFooterView = [self tableFooterView];
    
    //加载数据
    titles = @[@"昵称:",@"收货人:",@"邮箱:",@"邮寄地址:",@"手机号:"];
    placeholders = @[@"游客",@"请填写收货人姓名",@"您的邮箱",@"您的邮寄地址",@"未填写"];
    imgs = @[@"nicheng",@"nicheng",@"email",@"address",@"phonenumber"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [_carlistview refreshUI];
}



#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    NSString* kind = segue.identifier;
    if ([kind isEqualToString:@"modifyCarFromInfoView"]) {
        changecarViewController* nextview = [segue destinationViewController];
        nextview.car = (Car*)sender;
    }
    
}




#pragma mark - table view
#pragma mark Table Data Source Methods
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return 0;
    }else{
        return 10;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section==0){
        return 5;
    }
    else{
        return 1;
    }
}
#pragma mark Table Delegate Methods
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    __weak typeof(self) weakSelf = self;


    if(indexPath.section==0 && indexPath.row < 5){
        Input_LeftImage_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Input_LeftImage_Cell"];
        __weak typeof(Input_LeftImage_Cell) *weakcell = cell;
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"Input_LeftImage_Cell" owner:self options:nil] firstObject];
        }
        NSString* title = [titles objectAtIndex:indexPath.row];
        NSString* holder = [placeholders objectAtIndex:indexPath.row];
        NSString* imgname = [imgs objectAtIndex:indexPath.row];
        cell.editBeginBlock = ^(){
            if (indexPath.row > 2) {
                [UIView up:weakSelf.view length:100];
            }
        };
        cell.pressreturn = ^(NSString* str){
            if (indexPath.row > 2) {
                NSLog(@"resign");
                [UIView downWithTopBar:weakSelf.view];
                [weakcell resignFirstResponder];
            }
        };
        switch (indexPath.row) {
            case 0:
            {
                [cell configWithImgName:imgname andLabel:title andPlaceholder:holder andValue:_curUser.Nickname];
                cell.textValueChangedBlock = ^(NSString *valueStr){
                    self.curUser.Nickname = valueStr;
                };
            }
                break;
            case 1:
            {
                [cell configWithImgName:imgname andLabel:title andPlaceholder:holder andValue:_curUser.Shouhuoren];
                cell.textValueChangedBlock = ^(NSString *valueStr){
                    self.curUser.Shouhuoren = valueStr;
                };
            }
                break;
            case 2:
            {
                [cell configWithImgName:imgname andLabel:title andPlaceholder:holder andValue:_curUser.Email];
                cell.textValueChangedBlock = ^(NSString *valueStr){
                    self.curUser.Email = valueStr;
                };
            }
                break;
            case 3:
            {
                [cell configWithImgName:imgname andLabel:title andPlaceholder:holder andValue:_curUser.Address];
                cell.textValueChangedBlock = ^(NSString *valueStr){
                    self.curUser.Address = valueStr;
                };
            }
                break;
            case 4:
            {
                [cell configWithImgName:imgname andLabel:title andPlaceholder:holder andValue:_curUser.Phone];
                cell.textValueChangedBlock = ^(NSString *valueStr){
                    self.curUser.Phone = valueStr;
                };
            }
                break;
        }
        return cell;
    }
    else
    {
        LeftImage_RightButton_Cell *passcell = [tableView dequeueReusableCellWithIdentifier:@"LeftImage_RightButton_Cell"];
        if (passcell == nil) {
            passcell = [[[NSBundle mainBundle] loadNibNamed:@"LeftImage_RightButton_Cell" owner:self options:nil] firstObject];
        }
        [passcell configWithImgName:@"password2" andValue:@"修改密码" andButtonImgName:@"pencil"];
        passcell.buttonClicked = ^(){
            [self performSegueWithIdentifier:@"changepass" sender:self];
        };
        return passcell;
    }

}


- (UIView*)tableFooterView{
    
    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 90)];
    UIButton *saveBtn = [UIButton buttonWithStyle:StrapThemeStyle andTitle:@"保存" andFrame:CGRectMake(30, 25, kScreen_Width-30*2, 44) target:self action:@selector(saveInfo:)];
    [saveBtn setPopingPressed];
    
    [footerV addSubview:saveBtn];
    return footerV;
}


#pragma mark - Button Actions
- (void)saveInfo:(id)sender {
    //__weak typeof(self) weakSelf = self;
    
   // weakSelf.saveInfoButton.enabled = NO;
    [[NetAPIManager sharedManager] edit_UserInfo:[self.curUser toUpdateInfoParams] andBlock:^(id data, NSError *error) {
    //    weakSelf.saveInfoButton.enabled = YES;
        if (data) {
            NSLog(@"更新成功");
            [self showHudTipStr:@"更新成功"];
            [Login updateUser:_curUser];
        }else{
            [self showHudTipStr:@"更新失败"];
            NSLog(@"保存失败");
        }
    }];
}


- (IBAction)switchView:(id)sender {
    UISegmentedControl *segmentedControl = sender;
    NSInteger index = [segmentedControl selectedSegmentIndex];
    if(index==0){
        [self.view bringSubviewToFront:_userinfo];
    }
    else{
        [self.view bringSubviewToFront:_carinfo];
    }
    [self.view bringSubviewToFront:_segmentcontrol];

}


@end
